package com.cro.prison.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cro.prison.common.ErrorMessageMethod;
import com.cro.prison.entity.pojo.Depart;
import com.cro.prison.entity.pojo.Device;
import com.cro.prison.entity.pojo.ErrorMessage;
import com.cro.prison.entity.pojo.User;
import com.cro.prison.entity.vo.*;
import com.cro.prison.service.*;
import com.cro.prison.utils.Code;
import com.cro.prison.utils.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/message")
public class ErrorController {

    @Autowired
    private UserService userService;

    @Autowired
    private ErrorMessageService errorMessageService;
    @Autowired
    private QueryCriteriaService queryCriteriaService;

    @Autowired
    private DepartService departService;

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/listErrorByCriteria")
    @ResponseBody
    public Result listByQuery(@RequestBody QueryCriteria queryCriteria) {
        List<ErrorMessage> list = queryCriteriaService.listByQuery(queryCriteria);
        List<User> users = userService.listAll();
        List<Device> deviceList = deviceService.selectAll();
        List<Depart> departList = departService.list();
        List<ErrorMessageVo> messageVoList = ErrorMessageMethod.getMessageVoList(list, users, departList,deviceList);
        return new Result(Code.LOGIN_OK, messageVoList);
    }
    @PostMapping("/count")
    @ResponseBody
    public ArrayList<Integer> TypeCount(@RequestBody QueryCriteria queryCriteria) {
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            queryCriteria.setType(i);
            int Typecount = queryCriteriaService.selectByType(queryCriteria);
            integers.add(Typecount);
        }
        return integers;
    }
    @PostMapping("/listChartData")
    @ResponseBody
    public Result listChartData(@RequestBody QueryCriteria queryCriteria) {
        //得到近一星期的记录
        List<ErrorMessage> list = queryCriteriaService.listByQuery(queryCriteria);
        //按日期统计违规次数
        int[] dataTimes = errorMessageService.queryViolationRecords(list,queryCriteria.getStart());
        return new Result(200,dataTimes);
    }

    @GetMapping("/listAlarmItems")
    @ResponseBody
    public Result listAlarmItems() {
        //map ：key-》departId value—》depart，主要用于静态方法查询当前departId的最高级departId
        HashMap<Long, Depart> map = new HashMap<>();
        //key所有最高级depart的departId，value前端返回的item
        HashMap<Long, AlarmItem> resultMap = new HashMap<>();
        List<Depart> departList = departService.list();
        for (Depart depart : departList) {
            map.put(depart.getId(),depart);
            if(depart.getPid()==0){
                //将最高级的depart放入此map
                AlarmItem alarmItem = new AlarmItem();
                alarmItem.setDepartId(depart.getId());
                alarmItem.setLabel(depart.getName());
                alarmItem.setExceptionNum(0L);
                resultMap.put(depart.getId(),alarmItem);
            }
        }
        List<ErrorMessage> errorMessageList = errorMessageService.list(new QueryWrapper<ErrorMessage>().eq("status", 0));
        for (ErrorMessage errorMessage : errorMessageList) {
            Long departId = errorMessage.getDepartId();
            Long parentId = ErrorMessageMethod.findParent(departId, map);

            resultMap.get(parentId).setExceptionNum(resultMap.get(parentId).getExceptionNum()+1);
        }
        List<AlarmItem> result = resultMap.entrySet().stream().map(item -> {
            return item.getValue();
        }).collect(Collectors.toList());
        return new Result(200,result);
    }

    @GetMapping("/listErrorByDepart/{departId}")
    @ResponseBody
    public Result getErrorMessageListByDepartId(@PathVariable Long departId) {

        List<Depart> departList = departService.list();
        List<Device> deviceList = deviceService.selectAll();

        List<User> users = userService.listAll();
        QueryCriteria query = new QueryCriteria();
        query.setStatus(0);
        List<ErrorMessage> errorMessageList = queryCriteriaService.listByQuery(query);
        //List<ErrorMessage> errorMessageList = errorMessageService.list(new QueryWrapper<ErrorMessage>().eq("status", 0));

        errorMessageList = ErrorMessageMethod.getMessageVoListByDepartId(errorMessageList, departList, departId);
        //现在 resultMap中是对应所有的建筑物的异常
        return new Result(200,ErrorMessageMethod.getMessageVoList(errorMessageList,users,departList,deviceList));
    }

    @PostMapping("/updateErrorMessage")
    @ResponseBody
    public Result updateErrorMessage(@RequestBody UpdateErrorMessageVo updateErrorMessageVo){
        errorMessageService.updateErrorMessage(updateErrorMessageVo.getId(),updateErrorMessageVo.getDescription());
        return new Result(Code.UPDATE_OK);
    }

    @GetMapping("/SeriousErrCount")
    @ResponseBody
    public List<Integer> SeriousErrCount() {
        List<Integer> list = errorMessageService.SeriousErrList();
        return list;
    }

    @GetMapping("/getTotalErrCount")
    @ResponseBody
    public Result getTotalErrCount() {
        List<ErrorCountVo> list = errorMessageService.getTotalErrCount();
        return new Result(Code.GET_OK,list,null);
    }

}
